package com.shimao.celnet.payment.util;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

/**
 * @Author: MattChen
 * @Description: 文件解压工具包
 * @Date: 17:56 2019/5/21
 * @ModifiedBy:
 */
public class ZipUtils {

    /*
     * 功能描述: 此方法只解析文件夹下只有一个csv文件的zip压缩包，后期若要使用相关功能，建议重新写一个方法
     * @Author: Matt
     * @Date: 2019/5/22 10:30
     */
    public static String unZipFiles(File zipFile, String descDir)throws IOException {
        File pathFile = new File(descDir);
        if(!pathFile.exists()){
            pathFile.mkdirs();
        }
        ZipFile zip = new ZipFile(zipFile,"GBK");
        Enumeration entries = zip.getEntries();
        if(entries.hasMoreElements()){
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream in =zip.getInputStream(entry);
            String outPath =(descDir+String.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()))+".csv").replaceAll("\\*", "/");
            File file = new File(outPath);
            //输出文件路径信息 
            System.out.println(outPath);

            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while((len=in.read(buf1))>0){
                out.write(buf1,0,len);
            }
            in.close();
            out.close();
            return outPath;
        }else{
            throw new RuntimeException("解压通联对账文件失败");
        }
    }
}
